var formType;
var archid;
mini.parse();

var form = new mini.Form("archivesForm");

var divisionList;


function beforenodeselect(e) {
    //禁止选中父节点
    if (e.isLeaf == false) e.cancel = true;
}


$(function () {
    divisionList = JSON.parse(localStorage.getItem("divisionList"));
    if (divisionList == null) {
        initDivision();
        initDivisionData(divisionList, '');
    } else {
        initDivisionData(divisionList, '');
    }
})


function getDocker(e) {
    var o = form.getData(true);
    if (o.did === null || o.did === '') {
        mini.showTips({
            content: '请先选择区划',
            state: 'danger',
            x: 'Center',
            y: 'Top',
            timeout: 3000
        });
        return;
    }
    var nicknameBtn = this;
    mini.open({
        url: BaseUrl() + "/docker/archives/getDocker.html",
        showMaxButton: false,
        title: "选择负责医生",
        width: 650,
        height: 380,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { action: "choose", did: o.did };
            iframe.contentWindow.SetData(data);

        },
        ondestroy: function (action) {
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.GetData();
                data = mini.clone(data);
                if (data) {
                    nicknameBtn.setValue(data.uuid);
                    nicknameBtn.setText(data.nickname);
                }
            }
        }
    });

}

//身份证校验
function onIDCardsValidation(e) {
    if (e.isValid) {
        var pattern = /\d*/;
        if (e.value.length < 15 || e.value.length > 18 || pattern.test(e.value) == false) {
            e.errorText = "必须输入15~18位数字";
            e.isValid = false;
        }
    }
}


function initDivision() {
    $.ajax({
        url: GatewayUrl() + '/archives/divisionList',
        type: 'get',
        async: false,
        headers: {
            "Authorization": localStorage.getItem("token")
        },
        success: function (dt) {
            if (dt.code === 200) {
                localStorage.setItem("divisionList", JSON.stringify(dt.data));
                divisionList = dt.data;
            }
        }
    })
}


function initDivisionData(data, did) {
    var obj = mini.get("Division");
    obj.loadList(data, "did", "pid");
    if (did !== '') {
        obj.setValue(did)
    }
}

function SaveData() {
    var o = form.getData(true);
    form.validate();
    if (form.isValid() == false) return;
    $.ajax({
        url: GatewayUrl() + "/archives/add",
        type: 'post',
        contentType: "application/json",
        data: JSON.stringify(o),
        headers: {
            "Authorization": localStorage.getItem("token")
        },
        success: function (dt) {
            if (dt.code === 200) {
                CloseWindow("save");
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR.responseText);
            CloseWindow();
        }
    });
}


function UpdateData() {
    var o = form.getData();
    o.archid = archid;
    console.log(o);
    form.validate();
    if (form.isValid() == false) return;
    $.ajax({
        url: GatewayUrl() + "/archives/update",
        type: 'put',
        contentType: "application/json",
        data: JSON.stringify(o),
        headers: {
            "Authorization": localStorage.getItem("token")
        },
        success: function (dt) {
            if (dt.code === 200) {
                CloseWindow("save");
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR.responseText);
            CloseWindow();
        }
    });

}

////////////////////
//标准方法接口定义
function SetData(data) {
    if (data.action == "edit") {
        archid = data.archid;
        formType = 'edit';
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        $.ajax({
            url: GatewayUrl() + "/archives/info/" + data.archid,
            type: 'get',
            headers: {
                "Authorization": localStorage.getItem("token")
            },
            success: function (dt) {
                if (dt.code === 200) {
                    var o = mini.decode(dt.data.archives);
                    form.setData(o);
                    mini.get("nicknameBtn").setText(dt.data.data.nickname);
                }
            }
        })


    } else {
        formType = 'add';
    }
}



function GetData() {
    var o = form.getData();
    return o;
}
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    if (formType == 'add') {
        mini.confirm("确定添加？", "确定？",
            function (action) {
                if (action == "ok") {
                    SaveData();
                }
            }
        );
    } else {
        mini.confirm("确定修改？", "确定？",
            function (action) {
                if (action == "ok") {
                    UpdateData();
                }
            }
        );
    }

}
function onCancel(e) {
    CloseWindow("cancel");
}